'''
Created on Apr 12, 2012

@author: ke
'''

from google.appengine.ext import webapp
#from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
#from google.appengine.ext.db import djangoforms
from google.appengine.api import users
import urllib2
import numpy as np
import csv
from history import *
import datetime
import tools.AlchemyAPI as nlp
from bs4 import BeautifulSoup


class WordFrequency(webapp.RequestHandler):
    '''
    extract keywords from an article from web
    '''
    
    def get_keywords_from_url(self, url):
        '''
        return the keywords within a single url
        '''
        nlpObj = nlp.AlchemyAPI()
        nlpObj.loadAPIKey('login/alchemy_key.txt')
        keywords = nlpObj.URLGetRankedKeywords(url)
        
        soup = BeautifulSoup(keywords)
        results=[]
        
        for keyword in soup('keyword'):
            word = keyword('text')[0]
            word=word.contents[0]
            rel = keyword('relevance')[0]
            rel = rel.contents[0]
            results.append((word.encode('ascii', 'ignore'), float(rel)))
        
        return results
    
    def get_keywords_from_urls(self, urls):
        urls = urls.split(';')
        results = {}
        for url in urls:
            results.setdefault(url)
            results[url] = self.get_keywords_from_url(url)
        return results

    def get(self):
        
        
        #url = 'http://allthingsd.com/20120412/googles-q1-a-little-light/'
        #author = nlpObj.URLGetAuthor(url)
        #title = nlpObj.URLGetTitle(url)
        #result = nlpObj.URLGetText(url)
        #sentiment = nlpObj.URLGetTextSentiment(url)
        
        #concepts = nlpObj.URLGetRankedConcepts(url)
        #entities = nlpObj.URLGetRankedNamedEntities(url)
        
        html =template.render('templates/wordfrequency_form.html', {})
        self.response.out.write(html)
        
        

        #self.response.out.write(result)
        #self.response.out.write(author)
        #self.response.out.write(title)
        #self.response.out.write(result)
        #self.response.out.write(sentiment)
        #self.response.out.write(keywords)
        #self.response.out.write(concepts)
        #self.response.out.write(entities)
        
        
        
        
        
    
    def post(self):
        
        self.response.headers['Content-Type'] = 'text/plain'
        
        u = self.request.get('query_url').replace(' ','').lower().encode('ascii', 'ignore')
        self.response.out.write(u)
        #url = 'http://allthingsd.com/20120412/live-from-google-q1-earnings-a-new-class-of-stock-eight-years-after-going-public/'
        results = self.get_keywords_from_url(url=u)
        
        
        for item in results:
            self.response.out.write(item)
        
        new_history = WordFrequencyHistory()
        new_history.query_url = u
        new_history.user = users.get_current_user()
        new_history.query_date = datetime.date.today()
        
        new_history.put()
        